import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text,Checkbox } from '@tarojs/components'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import './login.scss'

export default class Index extends Component {
	constructor(props) {
    super(props);
    this.state = { 
    	phoneNumber:'',
    	verifyCode:'',
    	codestatus:"获取验证码",
        lesstime:60
    	
    }
  }
  componentWillMount () { 
  	
  }

  componentDidMount () { }

  componentWillUnmount () { }

  componentDidShow () { }

  componentDidHide () { }

  config = {
    navigationBarTitleText: '登录'
  }
  
  open(url){
	CommonJs.openTo(url)	
  }
  handleChange(name,e){

  	let value=e.target.value;
  	this.setState({
      [name]: value
    });
  }
  
  
  
 
  
  
  sure(){
  	var phoneNumber=this.state.phoneNumber;
  	var verifyCode=this.state.verifyCode;
  	if(verifyCode==""){
		CommonJs.msg('密码不能为空');
        return false;   
	}
  	var url='/loginSubmit'
  	var _this=this;
  	var date={
  		"name":phoneNumber,
	    "password":verifyCode,
  	}
	var dateinfo=Taro.getStorageSync('dateinfo')
	if(!dateinfo){
		dateinfo={};
	}
	if(dateinfo.introducer){
		date.introducer=dateinfo.introducer;
	}
	if(dateinfo.counselorId){
		date.counselorId=dateinfo.counselorId;
	}
  	CommonJs.ajaxRequest(url,date,'GET',function(ret){
  		dateinfo.userid=ret.id
		dateinfo.token=ret.token
		Taro.setStorage({key:'dateinfo',data:dateinfo})
		var userinfo={
			nickName:ret.nickName,
			avatarUrl:ret.photo
		}
		Taro.setStorage({key:'userinfo',data:userinfo})
		setTimeout(function(){
			if(!ret.consultantId||ret.consultantId<=0){
				/*判断是否需要更换顾问 */
				if(dateinfo.qid){
					_this.$preload({
						type:1,
						qid:dateinfo.qid
					})
					var urls = ''
					var par = _this.$router.params
					if (par.shareUid) { 
					  urls = '/pages/activity/activity?scene='+dateinfo.qid+'&shareUid='+par.shareUid
					} else if (par.activityId) {
					  urls = '/pages/activity/activity?scene='+dateinfo.qid+'&activityId='+par.activityId
					} else {
					  var goodsid=Taro.getStorageSync('goodsid')
					  if(goodsid&&goodsid!=''){
						if(goodsid.indexOf("|||")>=0){
							var arr=goodsid.split("|||");
							_this.$preload({
								type:arr[0],
								malltype:arr[1]
							})
						}
						else{
							_this.$preload({
								type:goodsid
							})
						  }
						Taro.setStorage({key:'goodsid',data:''}) 
						urls = '/pages/mall/goodsxq'
					  }
					  else{
						urls = '/pages/xijia/grouplist'
					  }
					}
					CommonJs.open(urls)
				}
				else{
					urls = '/pages/xijia/grouplist'
					CommonJs.open(urls)
				}
			}
			else{
				CommonJs.open('/pages/index/changegw?cid='+ret.consultantId+'&qid='+dateinfo.qid)
			}
	   },300)
	})
  }
  render () {
    return (
      <View className='page'>
        <View className='login'>
        	<View className='mobile'>
        		<View className='login-tit'>手机/账号登录</View>
        		<View className='login-mobile'>
        			<View className='fromdl'> 
		        		<View className='fromdd'><Input className='input01 input02' type="text" style="width:70%;" value='' onInput={this.handleChange.bind(this,'phoneNumber')} placeholder='请输入手机号/账号'/></View>
		        		<View className='fromdt'></View>
		        	</View>
		        	<View className='fromdl'>
		        		<View className='fromdd'><Input className='input01 input02' type="password" value='' onInput={this.handleChange.bind(this,'verifyCode')} placeholder='请输入密码'/></View>
		        	</View>
        		</View>
        		<View className='btn01' onClick={this.sure.bind(this)}>登录</View>
	        </View>
        </View>
      </View>
    )
  }
}
